import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'
import {resolve} from 'node:path'

const name = 'rl-element'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    vueDevTools(),
  ],
  build: {
    lib: {
      entry: resolve(__dirname, 'src/index.ts'), // 整库入口
      name: name,
      // Vite 会对每个 format 使用下面 fileName 回调
      formats: ['es', 'umd'],
      fileName: (format) => format === 'umd' ? `${name}.umd.cjs` : `${name}.esm.js`,
    },
    rollupOptions: {
      external: ['vue'],
      output: { 
        globals: { vue: 'Vue' } 
      }
    },
    sourcemap: true
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },
})
